home *** CD-ROM | disk | FTP | other *** search
- Path: anvil.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: macro that return value, how?
- Date: 29 Feb 1996 14:59:36 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4h5b4oINN8l1@anvil.ugrad.cs.ubc.ca>
- References: <3133e87b.868433@news.csus.edu>
- NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
-
- In article <3133e87b.868433@news.csus.edu>,
- Jerry Leong <wleong@sfsu.edu> wrote:
- >
- >Hi,
- > I need to define a macro that will first calculate a given a
- >parameter and then return it. How can I do that?
- >
- >p/s: Say I pass a value to the macro, macro will perform some
- >addition, then return the value.
- >
- >#define add1(x) { x+=VAR; return x; }
- >
- >something like that by the above does not compile.
-
- It's not standard C. The GCC compiler supports the construct, by the way. It
- does this by allowing statement blocks to act as expressions, whose value is
- that of the last statement.
-
- In standard C, you can do this using comma expressions. In your example, no
- comma expression is required, since x+=VAR is an expression-statement, whose
- value is that of x+VAR.
-
- #define add1(x) ((x) += VAR)
-
-
- If you want to do multi-step computation, you use a comma expression, whose
- members are guaranteed to evaluate left to right:
-
- #define addiv(x) ((x) += VAR1; (x) /= VAR2; (x)++)
-
- The result of addiv(v) is the value of v just before it is decremented in the
- final step. For instance, if v is initially 3, VAR1 is 1, and VAR2 is 2,
- the result of the function is 2, and v is set to 3.
- --
-
-